package com.superman.supermarket.client.view.refunddetail;

import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;
import com.superman.mvpframe.client.mvp.IPageViewPresenter;
import com.superman.mvpframe.client.utils.XJsonUtil;
import com.superman.supermarket.client.composite.orderlist.opt.OrderListOpButton;
import com.superman.supermarket.client.constant.OrderStatusConstants;
import com.superman.supermarket.client.constant.RefundStatusConstants;
import com.superman.supermarket.client.constant.RefundTypeConstants;
import com.superman.supermarket.client.widget.header.CommonPageHeader;
import com.superman.supermarket.client.widget.input.type.CanEmptyInput;

/**
 * 退款详情页面实现类
 * 
 * @author superman
 * @version 2018年8月20日下午10:08:30
 */
public class RefundDetailViewImpl extends Composite implements RefundDetailView {

	interface RefundDetailViewImplUiBinder extends UiBinder<Widget, RefundDetailViewImpl> {
	}

	private static RefundDetailViewImplUiBinder uiBinder = GWT.create(RefundDetailViewImplUiBinder.class);

	@UiField
	FlowPanel bottompanel;
	@UiField
	FlowPanel historypanel;
	@UiField
	HTML orderstatushtml, orderidhtml;
	@UiField
	CommonPageHeader pageheader;
	private RefundDetailViewPresenter presenter;

	@UiField
	CanEmptyInput typeinput, reasoninput, priceinput, memoinput, timeinput;

	/**
	 * 构造方法
	 * 
	 * @author superman
	 * @version 2018年8月20日 下午10:08:55
	 */
	public RefundDetailViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));

		priceinput.getInputbase().getElement().getStyle().setProperty("color", "red");

		historypanel.addDomHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				presenter.pushToRefundProcessPage();
			}
		}, ClickEvent.getType());
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub

	}

	@Override
	public void setPageHeaderText(String text) {
		pageheader.setText(text);
	}

	@Override
	public void setPageViewPresenter(IPageViewPresenter presenter) {
		this.presenter = (RefundDetailViewPresenter) presenter;
	}

	@Override
	public void setRefundInfo(Map<String, Object> map) {
		if (map.get("REFUND_STATE") == null) {
			int status = (int) Double.parseDouble(String.valueOf(map.get("ORDER_STATE")));
			orderstatushtml.setText(OrderStatusConstants.getDescriptionByStatus(status));
		} else {
			int refundstatus = (int) Double.parseDouble(String.valueOf(map.get("REFUND_STATE")));
			orderstatushtml.setText(RefundStatusConstants.getDescByStatus(refundstatus));
		}
		orderidhtml.setHTML("订单编号:&nbsp;&nbsp;&nbsp;&nbsp;" + String.valueOf(map.get("ORDER_ID")));

		int refundType = (int) Double.parseDouble(String.valueOf(map.get("REFUND_TYPE")));
		if (refundType == RefundTypeConstants.REFUND_ONLY_MONEY) {
			typeinput.setValue("仅退款");
		} else {
			typeinput.setValue("退货退款");
		}
		reasoninput.setValue(String.valueOf(map.get("REASON")));
		priceinput.setValue(String.valueOf(map.get("REFUND_FEE")));
		timeinput.setValue(String.valueOf(map.get("APPLY_TIME")));
		if (map.get("MEMO") != null)
			memoinput.setValue(String.valueOf(map.get("MEMO")));
		else
			memoinput.setValue("没有填写");

		// 可操作的按钮
		if (map.get("OP") == null || XJsonUtil.getOnlyList((JSONArray) map.get("OP")).size() == 0)
			bottompanel.removeFromParent();
		else {
			bottompanel.clear();
			List<Object> ops = XJsonUtil.getOnlyList((JSONArray) map.get("OP"));
			for (Object object : ops) {
				int op = (int) Double.parseDouble(String.valueOf(object));
				OrderListOpButton orderListOpButton = new OrderListOpButton(map, op);
				bottompanel.add(orderListOpButton);
			}
		}
	}

}
